// pages/01homePage/03foodOrder/foodOrder.js
var app = getApp()
var appData = app.globalData
Page({
   data: {
      order: '',
      foodNum: 0,
      totalPrice: 0,
   },

   onLoad: function (options) {
      var obj = JSON.parse(options.order)
      this.countNumAndPrice(obj)
   },

   countNumAndPrice: function (obj) {
      var num = 0
      var price = 0
      for (var i = 0; i < obj.order.length; i++) {
         num += obj.order[i].num * 1
         price += (obj.order[i].price * obj.order[i].num)
      }
      this.setData({
         order: obj,
         foodNum: num,
         totalPrice: price
      })
   },

   addFood: function () {
      wx.navigateBack()
   },

   submitOrder: function (e) {
      var e_v = e.detail.value
      if (e_v.renshu == 0) {
         app.toast('请输入用餐人数')
         return
      }
      var param = this.data.order
      param.uid = appData.shopInfo.shopInfo.userid
      param.guest_id = appData.guestInfo.userid
      param.renshu = e_v.renshu
      param.dec = e_v.dec
      param.totalPrice = this.data.totalPrice
      param.seat_fee = appData.shopInfo.shopInfo.seat_fee
      var that = this
      app.getJsonData('api/space/table_eat.php', param, function (res) {
         that.readySendSocket(param, res.data)
         app.noticeUpdateTableList(res.data)
         app.noticeTicket(param.zhuo)
         wx.navigateBack({ delta: 2 })
      })
   },
   //准备发送wss
   readySendSocket: function (p, rd) {
      appData.isWsSender = 1
      //msg数组结构：0桌号, 1状态, 2订单号, 3订单价格, 4点菜数量,5人数
      var m = {
         msg: rd,
         setMsg: p.fenlei + p.zhuohao + '桌开席了'
      }
      console.log('准备发送wss的m==', m)
      var json = {
         type: 'say',
         to_client_id: 'all',
         content: JSON.stringify(m)
      }
      var msg = JSON.stringify(json)
      app.sendSocketMsg(msg)
   },
})